﻿// -----------------------------------------------------------------------
// <copyright file="DbGenerator.cs" company="">
// TODO: Update copyright text.
// </copyright>
// -----------------------------------------------------------------------

namespace TaleGeneratorModel
{
	using System;
	using System.Collections.Generic;
	using System.Linq;
	using System.Text;
	using System.Reflection;
	using System.IO;
	using System.Data.SqlServerCe;

	/// <summary>
	/// TODO: Update summary.
	/// </summary>
	public class DbGenerator
	{
		public void CreateDatabase(string filePath)
		{
			StreamReader scriptStream =new StreamReader( Assembly.GetExecutingAssembly().GetManifestResourceStream("TaleGeneratorModel.DbGenerationScript.sql"));
			string fullScript = scriptStream.ReadToEnd();
			string[] scripts = fullScript.Split(';');
			string fullDbPath = "Data Source=" + filePath;
			SqlCeEngine engine = new SqlCeEngine(fullDbPath);
			if (File.Exists(filePath))
				File.Delete(filePath);
			engine.CreateDatabase();
			engine.Dispose();
			SqlCeConnection connect = new SqlCeConnection(fullDbPath);
			SqlCeCommand cmd = new SqlCeCommand();
			cmd.Connection = connect;
			connect.Open();
			for (int i = 0; i < scripts.Length; i++)
				if (!string.IsNullOrWhiteSpace(scripts[i]))
				{
					cmd.CommandText = scripts[i];
					cmd.ExecuteNonQuery();
				}
			
			connect.Close();
		}
	}
}
